package agriculture.exception;

import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Map;

@RestControllerAdvice
public class BusinessExceptionHandler {

    // 处理 DuplicateKeyException 时模糊提示
    @ExceptionHandler(DuplicateKeyException.class)
    public ResponseEntity<?> handleDuplicateKey() {
        return ResponseEntity.status(409)
                .body(Map.of("code", 409, "msg", "系统繁忙，请稍后重试"));
    }

    // BusinessException 提供明确业务提示
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<?> handleBusiness(BusinessException ex) {
        return ResponseEntity.badRequest()
                .body(Map.of("code", 400, "msg", ex.getMessage()));
    }
}
